package jgl.vajra.kingkong.widget.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2016/3/21.
 */
public class LongTimeUtil {

    public static String getTime(long time) {
        Calendar newCalendar = Calendar.getInstance();
        Calendar oldCalendar = Calendar.getInstance();
        oldCalendar.setTime(new Date(time));
        if (newCalendar.get(Calendar.YEAR) > oldCalendar.get(Calendar.YEAR)) {
            return getFormatTime(time);
        } else if (newCalendar.get(Calendar.MONTH) > oldCalendar.get(Calendar.MONTH)) {
            return (newCalendar.get(Calendar.MONTH) - oldCalendar.get(Calendar.MONTH)) + "月前";
        } else if (newCalendar.get(Calendar.DAY_OF_MONTH) > oldCalendar.get(Calendar.DAY_OF_MONTH)) {
            return (newCalendar.get(Calendar.DAY_OF_MONTH) - oldCalendar.get(Calendar.DAY_OF_MONTH)) + "天前";
        } else if (newCalendar.get(Calendar.HOUR) > oldCalendar.get(Calendar.HOUR)) {
            return (newCalendar.get(Calendar.HOUR) - oldCalendar.get(Calendar.HOUR)) + "小时前";
        } else if (newCalendar.get(Calendar.MINUTE) > oldCalendar.get(Calendar.MINUTE)) {
            return (newCalendar.get(Calendar.MINUTE) - oldCalendar.get(Calendar.MINUTE)) + "分钟前";
        } else {
            return "刚刚";
        }
    }

    private static String getFormatTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date(time));
    }

    /**
     * Long类型的时间转换成：n秒前/n分钟前/n小时前/日期
     *
     * @param time   long类型的时间
     * @param format 很长时间前显示的日期格式
     * @return
     */
    public static String dataLongToSNS(long time, String format) {
        long now = System.currentTimeMillis();

        long diff = now - time;
        diff = diff / 1000;// 秒

        if (diff < 0) {
            return dateLongToString(time, format);
        }

        if (diff < 30) { // 30秒
            return "刚刚";
        }

        if (diff < 60) {
            return String.format("%s秒前", diff);
        }

        if (diff < 3600) {
            return String.format("%s分钟前", diff / 60);
        }
        //获取今天凌晨时间
        long todayStart = getMorning(new Date()).getTime();

        if (time >= todayStart) {// 今天
            return String.format("%s小时前", diff / 3600);
        }

        if (time < todayStart && time >= todayStart - 86400000) {
            return "昨天 " + dateLongToString(time, "HH:mm");
        }
        return dateLongToString(time, format);
    }

    //获取今天凌晨的时间
    private static Date getMorning(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static String dateLongToString(long time) {
        return dateLongToString(time, null);
    }

    public static String dateLongToString(long time, String format) {
        if (time <= 0) {
            return "Empty";
        }
        DateFormat format2 = new SimpleDateFormat(format);
        String dateString = format2.format(new Date(time * 1000));//此处一定要记得*1000 否则显示1970年的时间
        return dateString;
    }

    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
            return res;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }
}
